<?php
namespace app\admin\controller;
use think\Db;
class ModelField extends Common
{
    public function lst()
    {
        $prefix=config('database.prefix');
        $model_name=config('database.prefix').'model';//获取标签前缀
    	$modelfield=db('model_field')->alias('a')->field('a.*,b.model_name')->join("$model_name b",'a.model_id=b.id')->paginate(6);
        $this->assign(array(
            'modelfield'=>$modelfield,
            ));
    	return view('list');
    }

    //ajax异步删除字段
    public function del(){
        
            $fieldid=input('fieldid');
            $prefix=config('database.prefix');
            $model_name=config('database.prefix').'model';//获取标签前缀
            //模型表与字段表联查所需要的表名和字段
            $modelRes=db('model_field')->field('a.field_enname,b.table_name')->alias('a')->join("$model_name b ",'a.model_id=b.id')->find($fieldid);
            //dump($modelRes);die;
            $tableName=config('database.prefix').$modelRes['table_name'];//需要删除的表名
            $fieldName=$modelRes['field_enname'];//需要删除的字段
            $del=db('model_field')->delete($fieldid);
            //删除表的列的sql语句
            $sql="alter table {$tableName} drop  column {$fieldName}";
            $res=Db::execute($sql);//返回值是零
                if($del){
                    echo 1;//删除模型和附加表成功！
                }else{
                    echo 2;//删除模型和附加表成功！
                }
    
    }
    
    public function add()
    {
        if(request()->isPost()){
            $data=input('post.');
            if($data['model_id'] == ''){
                 $this->error('请选择所属模型');
            }    
            $name=db('model')->where('id',$data['model_id'])->column('table_name');
            $tableName=config('database.prefix').$name[0];
            //dump($tableName);die;
            //判断可选值是否是英文符号分割，不是就修改
            if($data['field_values']){
                $data['field_values']=str_replace('，', ',', $data['field_values']);
            }
            //数据验证
            $validate = validate('ModelField');//助手函数
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }  
            $add=db('model_field')->insert($data);
            if($add){
                //<!-- 1,文本输入框；2单选按钮3,复选框4,下拉菜单5,文本域6,附件类型 7浮点型 8、整形 9.长文本类型longtext-->
                //判断字段类型
                switch ($data['field_type']) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 6:
                        $fieldType= "varchar(150) not null default ''";
                        break;
                    
                    case 5:
                        $fieldType= "varchar(800) not null default ''";
                        break;
                    case 7:
                        $fieldType= "float not null default '0.0'";
                        break;
                    case 8:
                        $fieldType= "int not null default '0'";
                        break;
                    case 7:
                        $fieldType= "longtext";
                        break;
                    default:
                         $fieldType= "varchar(150) not null default ''";
                        break;
                }
                //$sql="alter table xxx add xxx varchar(150) not null default ''";表添加字段样式
                $sql="alter table {$tableName} add {$data['field_enname']} {$fieldType} comment '{$data['field_cnname']}'";
                //dump($sql);die;
                Db::execute($sql);
                $this->success('添加字段成功！',url('ModelField/lst'));
            }else{
                $this->error('添加字段失败！');
            }
            return;
        }
    	//模型数据
        $modelRes=db('model')->field('id,model_name')->select();
        $this->assign(array(
              'modelRes'=>$modelRes,  
            ));
    	return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data=input('post.');

            $prefix=config('database.prefix');
            $model_name=config('database.prefix').'model';//获取标签前缀
            //模型表与字段表联查所需要的表名和字段
            $modelRes=db('model_field')->field('a.field_enname,b.table_name')->alias('a')->join("$model_name b ",'a.model_id=b.id')->find($data['id']);
            $tableName=config('database.prefix').$modelRes['table_name'];//需要修改的表名
            $oldfieldName=$modelRes['field_enname'];//需要修改的原来的字段
            $fieldName=$data['field_enname'];//需要修改新的字段
            //<!-- 1,文本输入框；2单选按钮3,复选框4,下拉菜单5,文本域6,附件类型 7浮点型 8、整形 9.长文本类型longtext-->
                //判断字段类型
            switch ($data['field_type']) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 6:
                        $fieldType= "varchar(150) not null default ''";
                        break;
                    
                    case 5:
                        $fieldType= "varchar(800) not null default ''";
                        break;
                    case 7:
                        $fieldType= "float not null default '0.0'";
                        break;
                    case 8:
                        $fieldType= "int not null default '0'";
                        break;
                    case 7:
                        $fieldType= "longtext";
                        break;
                    default:
                         $fieldType= "varchar(150) not null default ''";
                        break;
                }
            $sql="alter table {$tableName} change {$oldfieldName} {$fieldName} {$fieldType} ";
            //dump($sql);die;
            Db::execute($sql);

            //判断可选值是否是英文符号分割，不是就修改
            if($data['field_values']){
                $data['field_values']=str_replace('，', ',', $data['field_values']);
            }
            //数据验证
            $validate = validate('ModelField');//助手函数
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            } 
            //执行修改操作
           $save=db('model_field')->update($data);
               if($save){

                    $this->success('修改字段成功！',url('ModelField/lst'));
               }else{
                    $this->error('修改字段失败！');
               }
            return;
        }
        //字段编辑需要的数据
        $data=db('model_field')->where('id',input('id'))->find();
        //模型数据
        $modelRes=db('model')->field('id,model_name')->select();
        $this->assign(array(
              'modelRes'=>$modelRes, 
              'data'=>$data, 
            ));
        return view();
    	
    }

}
